package com.bs.demo.config;

import com.bs.demo.interceptor.DemoRestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author bingshao
 * @date 2022/9/21
 **/
@Configuration("demoWebConfig")
public class WebConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getDemoRestInterceptor()).addPathPatterns(getIncludePathPatterns());
    }

    @Bean
    DemoRestInterceptor getDemoRestInterceptor() {
        return new DemoRestInterceptor();
    }

    /**
     * 需要拦截的url
     *
     * @return 需要拦截的url
     */

    private ArrayList<String> getIncludePathPatterns() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/demo/*"
        };
        Collections.addAll(list, urls);
        return list;
    }
}
